/**
 * 作者: wgx
 * 日期: 2025-05-19 09:36:20
 * 备注: 用户盛典派对的弹窗记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserFeastPopup;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserFeastPopupModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.feast_dbid";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_NOT;// 数据入库类型 不入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected long feastDbid; // server_feast的id
    protected long popupTime; // 弹出时间 (超过1天的可以删除)

    public void reset() {
    }

    protected String getKey() {
        return "UserFeastPopup." + userId + "." + feastDbid;
    }

    protected String getListKey() {
        return "UserFeastPopup." + userId;
    }

    protected String getListItemKey() {
        return feastDbid + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_feast_popup"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("feastDbid", feastDbid); // server_feast的id
        obj.put("popupTime", popupTime); // 弹出时间 (超过1天的可以删除)
        return obj;
    }

    public PBUserFeastPopup.Builder takeInitBuilder() {
        PBUserFeastPopup.Builder sb = PBUserFeastPopup.newBuilder();
        if (feastDbid != 0) {
            sb.setFeastDbid(feastDbid);
        }
        return sb;
    }

    public PBUserFeastPopup.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserFeastPopup.Builder sb = PBUserFeastPopup.newBuilder();
        if (isFieldUpdate("feastDbid")) {
            sb.setFeastDbid(feastDbid);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getFeastDbid() {
        return feastDbid;
    }

    public void putFeastDbid(long feastDbid) {
        if (this.feastDbid != feastDbid) {
            this.feastDbid = feastDbid;
            updateRedisHashField(getKey(), "feastDbid", this.feastDbid);
        }
    }

    public void addFeastDbid(long value) {
        this.feastDbid += value;
        updateRedisHashField(getKey(), "feastDbid", this.feastDbid);
    }

    public long getPopupTime() {
        return popupTime;
    }

    public void putPopupTime(long popupTime) {
        if (this.popupTime != popupTime) {
            this.popupTime = popupTime;
            updateRedisHashField(getKey(), "popupTime", this.popupTime);
        }
    }

    public void addPopupTime(long value) {
        this.popupTime += value;
        updateRedisHashField(getKey(), "popupTime", this.popupTime);
    }

}